//
//  BLGiftListView.m
//  Byelone
//
//  Created by Qi Liu on 2021/3/2.
//

#import "BLGiftListView.h"


@interface BLGiftListView ()
@property (nonatomic , strong) UILabel *showMoreLabel;

@property (nonatomic , strong) CAShapeLayer *shapeLayer;

@property (nonatomic , strong) UIActivityIndicatorView *indicatorView;

@property (nonatomic , strong) UIBezierPath *bezierPath;
/// 移动到多少时，才会触发
@property (nonatomic , assign) CGFloat maxCanBig;
@end


/// 形变的程度
#define Change_Distance    32

@implementation BLGiftListView

- (void)setContentSizeWidth:(CGFloat)contentSizeWidth {
    _contentSizeWidth = contentSizeWidth > BL_WINDOW_WIDTH ? contentSizeWidth : BL_WINDOW_WIDTH;
    self.contentSize = CGSizeMake(contentSizeWidth, 0);
    _maxCanBig = _contentSizeWidth - BL_WINDOW_WIDTH;
    
    [self.layer addSublayer:self.shapeLayer];
    _shapeLayer.frame = CGRectMake(_contentSizeWidth, 0, BL_WINDOW_WIDTH, self.frame.size.height);
    self.shapeLayer.path = self.bezierPath.CGPath;
    [self.shapeLayer addSublayer:self.showMoreLabel.layer];
}



- (UILabel *)showMoreLabel {
    if (!_showMoreLabel) {
        _showMoreLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 0, 10, self.frame.size.height)];
        _showMoreLabel.text = @"查\n看\n更\n多\n";
        _showMoreLabel.numberOfLines = 4;
        _showMoreLabel.font = [UIFont systemFontOfSize:10];
        _showMoreLabel.textColor = [UIColor colorWithRed:129/255.0 green:129/255.0 blue:129/255.0 alpha:1];
    }
    return _showMoreLabel;
}


- (CAShapeLayer *)shapeLayer {
    if (!_shapeLayer) {
        _shapeLayer = [CAShapeLayer layer];
        _shapeLayer.fillColor = [UIColor colorWithRed:237/255.0 green:237/255.0 blue:237/255.0 alpha:1].CGColor;
        _shapeLayer.borderColor = [UIColor blackColor].CGColor;
    }
    return _shapeLayer;
}


- (UIBezierPath *)bezierPath {
    if (!_bezierPath) {
        UIColor *color = [UIColor grayColor];
        [color set];
        
        _bezierPath = [UIBezierPath bezierPath];
        [_bezierPath moveToPoint:CGPointMake(0, 0)];
        //----- 弧线 -----//
        [_bezierPath addQuadCurveToPoint:CGPointMake(0, self.frame.size.height) controlPoint:CGPointMake(0, self.frame.size.height / 2)];
        [_bezierPath addLineToPoint:CGPointMake(_contentSizeWidth, self.frame.size.height)];
        [_bezierPath addLineToPoint:CGPointMake(_contentSizeWidth, 0)];
        [_bezierPath closePath];
    }
    return _bezierPath;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    //需要画半圆的话打开下面的设置
    //    BOOL toBig = NO;
    //    if (scrollView.contentOffset.x > _maxCanBig) {
    //        toBig = YES;
    //    }
    //    if (toBig) {
    //        [self setNeedsDisplay];
    //    }
    
    
    //检测左测滑动,开始加载更多
    if(scrollView.contentOffset.x +scrollView.width - scrollView.contentSize.width >30){
        if (self.indicatorView == nil) {
            UIActivityIndicatorView *indicatorView = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(scrollView.width - 20, scrollView.y + scrollView.height/2 - 10, 20, 20)];
            indicatorView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
            indicatorView.hidesWhenStopped = YES;
            self.indicatorView = indicatorView;
            [self.indicatorView stopAnimating];
            [scrollView.superview addSubview:self.indicatorView];
            
        }
        if (!self.indicatorView.isAnimating) {
            scrollView.x = -30;
            [self.indicatorView startAnimating];
            if (_dataReload) {
                _dataReload;
            }
            [self performSelector:@selector(stopRefresh) withObject:nil afterDelay:1];
        }
    }
}

-(void)stopRefresh{
    if (_indicatorView && _indicatorView.animating) {
        [_indicatorView stopAnimating];
        [UIView animateWithDuration:0.3 animations:^{
            self.x = 0;
        }];
    }
}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
    [_bezierPath removeAllPoints];
    
    NSLog(@"画画开始---%.f",self.contentOffset.x - _maxCanBig);
    UIColor *color = [UIColor grayColor];
    [color set];
    
    
    CGFloat pointX = 0;
    
    if (self.contentOffset.x - _maxCanBig < 0) {
        pointX = 0;
    } else {
        pointX = self.contentOffset.x - _maxCanBig;
    }
    
    
    CGFloat pointControlX = 0;
    
    CGFloat fixPointControlX = 0;
    
    if (self.contentOffset.x - _maxCanBig < 0) {
        pointControlX = 0;
    } else {
        
        if (self.contentOffset.x - _maxCanBig > Change_Distance) {
            fixPointControlX = (self.contentOffset.x - _maxCanBig) - Change_Distance;
            pointControlX = - (Change_Distance - fixPointControlX);
            
            self.showMoreLabel.frame = CGRectMake(10 + fixPointControlX, self.showMoreLabel.frame.origin.y, self.showMoreLabel.frame.size.width, self.showMoreLabel.frame.size.height);
            
        } else {
            self.showMoreLabel.frame = CGRectMake(10, self.showMoreLabel.frame.origin.y, self.showMoreLabel.frame.size.width, self.showMoreLabel.frame.size.height);
            pointControlX = - (self.contentOffset.x - _maxCanBig);
        }
    }
    
    [_bezierPath moveToPoint:CGPointMake(pointX, 0)];
    [_bezierPath addQuadCurveToPoint:CGPointMake(pointX, self.frame.size.height) controlPoint:CGPointMake( pointControlX, self.frame.size.height / 2)];
    [_bezierPath addLineToPoint:CGPointMake(_contentSizeWidth, self.frame.size.height)];
    [_bezierPath addLineToPoint:CGPointMake(_contentSizeWidth, 0)];
    
    [_bezierPath closePath];
    
    _shapeLayer.path = _bezierPath.CGPath;
    
}

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
